/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import controlador.BeanDetalle;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.clase_producto;
import controlador.BeanDetalle;

/**
 *
 * @author manuell2
 */
@WebServlet(name = "ser_rellena", urlPatterns = {"/ser_rellena"})
public class ser_rellena extends HttpServlet {
    Enumeration parametros;
    String parametro;
    String valor;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ser_rellena</title>");            
out.println("</head>");
out.println("<body>");
out.println("<h2><a href=\"http://localhost:8080/JSP_Plus/ServletConsulta\">INICIO</a></h2>");
out.println("<h1>Servlet ser_rellena at " + request.getContextPath() + "</h1>");
out.println("<table border=2>");
            //...
            parametros = request.getParameterNames();
            while (parametros.hasMoreElements()) {                
                parametro=(String) parametros.nextElement();
                valor= request.getParameter(parametro);
                out.println("<tr><td>nombre: "+ parametro + "<input type=\"submit\" "
                        + "value=\""
                        + valor
                        + "\" "
                        + "name=\"b_nombre\" /></td>");
                out.println("<td>valor: "+ valor + "</td></tr>");
                out.println("</table><br><br>");
            }
            rell(request, response);
            //....
            if (request.getParameter("b_modifica")!=null){
                out.println("Has pulsado b_modifica");
                // ejecutar sentencia modificar (update) y cargar el bean
                BeanDetalle bProducto=clase_producto.ModificaProducto(request.getParameter("String_id"), request.getParameter("String_nombre"), request.getParameter("String_cantidad"), request.getParameter("String_precio"));
		if(bProducto!=null){}
                out.println("Modificacion Completada");
            }
            
out.println("</body>");
out.println("</html>");
        } finally {            
            out.close();
        }
    }
//==============================================================================
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    //==========================================================================
    public void rell(HttpServletRequest request, HttpServletResponse response) throws IOException{
    BeanDetalle bProducto=clase_producto.rellenaXnombre(valor);
		if(bProducto!=null){
                    PrintWriter out=response.getWriter();
		    out.println("Codigo Encontradooooooooo.<br>");
//                    String sentencia=modelo.clase_producto.getSentencia();
//                    out.println("<br>Sentencia:"+sentencia);
                   
//                    out.println("<br>Id: "+modelo.clase_producto.get_id());
//                    out.println("<br>Nombre: "+modelo.clase_producto.get_nombre());
//                    out.println("<br>Cantidad: "+modelo.clase_producto.getCantidad());
//                    out.println("<br>precio: "+modelo.clase_producto.get_precio());
              out.println("<form action=\"ser_rellena\" method=\"get\">");      
                    out.println("<input type=\"text\" name=\"String_id\"  value=\""
                            + modelo.clase_producto.get_id()
                            + "\"   size=\"10\" /> \n" +
"            <input type=\"text\" name=\"String_nombre\" value=\""
                            + modelo.clase_producto.get_nombre()
                            + "\"   size=\"10\" /> \n" +
"            <input type=\"text\" name=\"String_cantidad\" value=\""
                            + modelo.clase_producto.getCantidad()
                            + "\"   size=\"10\" /> \n" +
"            <input type=\"text\" name=\"String_precio\" value=\""
                            + modelo.clase_producto.get_precio()
                            + "\"   size=\"10\" /> ");
                    
                    out.println("<input type=\"submit\" value=\"Modifica\" name=\"b_modifica\">");
                    out.println("</form>");
                }else{
			PrintWriter out=response.getWriter();
			out.println("Error, no se encontro el Codigo.");
		}
    }
//==============================================================================
}
